using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Sample.Outlook.CRMIntegration.ItemForms
{
	internal sealed partial class OpportunityAssoicationsControl : UserControl
	{
		private Guid? _accountID;
		private Guid _opportunityID;

		Dictionary<int, Guid> _mapIndexToContactID = new Dictionary<int, Guid>();
		Dictionary<int, Guid> _mapIndexToActivityID = new Dictionary<int, Guid>();

		public OpportunityAssoicationsControl(Guid? accountID, Guid opportunityID)
		{
			InitializeComponent();
			_accountID = accountID;
			_opportunityID = opportunityID;
		}

		private void OpportunityAssoicationsControl_Load(object sender, EventArgs e)
		{
			LoadOpportunityAssociations();
		}

		private void LoadOpportunityAssociations()
		{
			listContacts.Items.Clear();
			_mapIndexToContactID.Clear();
			listActivities.Items.Clear();
			_mapIndexToActivityID.Clear();

			if (!_accountID.HasValue)
				return;

			contactAssociationsTableAdapter.FillByOpportunity(associationsDataSet.ContactAssociations, _opportunityID, _accountID.Value);
			foreach (AssociationsDataSet.ContactAssociationsRow row in associationsDataSet.ContactAssociations)
			{
				int i = listContacts.Items.Add(
					string.Format("{0} {1}", row.FirstName, row.LastName),
					row.IsAssociated);
				_mapIndexToContactID.Add(i, row.LocalID);
			}

			activityAssociationsTableAdapter.FillByOpportunity(associationsDataSet.ActivityAssociations, _opportunityID, _accountID.Value);
			foreach (AssociationsDataSet.ActivityAssociationsRow row in associationsDataSet.ActivityAssociations)
			{
				int i = listActivities.Items.Add(row.Name, row.IsAssociated);
				_mapIndexToActivityID.Add(i, row.LocalID);
			}
		}

		public void ChangeAccount(Guid accountID)
		{
			_accountID = accountID;
			LoadOpportunityAssociations();
		}

		public void Save()
		{
			List<Guid> checkedContacts = new List<Guid>();
			foreach (int i in listContacts.CheckedIndices)
				checkedContacts.Add(_mapIndexToContactID[i]);

			UpdateContacts(checkedContacts);


			List<Guid> checkedActivities = new List<Guid>();
			foreach (int i in listActivities.CheckedIndices)
				checkedActivities.Add(_mapIndexToActivityID[i]);

			UpdateActivities(checkedActivities);
		}

		private void UpdateContacts(List<Guid> checkedContacts)
		{
			CrmDataSetTableAdapters.OpportunityContactsTableAdapter contactsAdapter = new CrmDataSetTableAdapters.OpportunityContactsTableAdapter();
			CrmDataSet.OpportunityContactsDataTable contactsTable = new CrmDataSet.OpportunityContactsDataTable();
			contactsAdapter.FillByOpportunityID(contactsTable, _opportunityID);

			foreach (CrmDataSet.OpportunityContactsRow row in contactsTable)
			{
				if (checkedContacts.Contains(row.LocalID) == false)
					row.Delete();
				else
					checkedContacts.Remove(row.LocalID);
			}

			foreach (Guid contactID in checkedContacts)
			{
				CrmDataSet.OpportunityContactsRow row = contactsTable.NewOpportunityContactsRow();
				row.LocalID = Guid.NewGuid();
				row.OpportunityID = _opportunityID;
				row.ContactID = contactID;

				contactsTable.AddOpportunityContactsRow(row);
			}

			contactsAdapter.Update(contactsTable);
		}

		private void UpdateActivities(List<Guid> checkedActivities)
		{
			CrmDataSetTableAdapters.OpportunityActivityTableAdapter opportunitiesAdapter = new CrmDataSetTableAdapters.OpportunityActivityTableAdapter();
			CrmDataSet.OpportunityActivityDataTable opportunitiesTable = new CrmDataSet.OpportunityActivityDataTable();
			opportunitiesAdapter.FillByOpportunityID(opportunitiesTable, _opportunityID);

			foreach (CrmDataSet.OpportunityActivityRow row in opportunitiesTable)
			{
				if (checkedActivities.Contains(row.LocalID) == false)
					row.Delete();
				else
					checkedActivities.Remove(row.LocalID);
			}

			foreach (Guid activityID in checkedActivities)
			{
				CrmDataSet.OpportunityActivityRow row = opportunitiesTable.NewOpportunityActivityRow();
				row.LocalID = Guid.NewGuid();
				row.ActivityID = activityID;
				row.OpportunityID = _opportunityID;

				opportunitiesTable.AddOpportunityActivityRow(row);
			}

			opportunitiesAdapter.Update(opportunitiesTable);
		}

	}
}
